﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Windows.Interop
Imports Co9320.NamaAlert.Grouping
Imports Xceed.Wpf.DataGrid

Class MainWindow

    ' TODO 参照しない方法でコード書く
    Private WithEvents _viewModel As ViewModel.MainWindowViewModel

    Protected ReadOnly Property ViewModel() As ViewModel.MainWindowViewModel
        Get
            If _viewModel Is Nothing Then
                _viewModel = DirectCast(Me.DataContext, ViewModel.MainWindowViewModel)
            End If
            Return _viewModel
        End Get
    End Property

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        Dim source = DirectCast(PresentationSource.FromDependencyObject(Me), HwndSource)
        source.AddHook(New HwndSourceHook(AddressOf WindowProc))


    End Sub

    Private Sub LiveProgramDataGrid_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles LiveProgramDataGrid.Loaded

        'Dim groupingItem = New GroupingItem
        'groupingItem.GroupDescriptions.Add(New ProgramGroupDescription(ProgramGroup.None, _
        '                                                               ViewModel.UISettings.DataGrid.GroupByAlertedProgram, _
        '                                                               ViewModel.UISettings.DataGrid.GroupByAlertTarget, _
        '                                                               ViewModel.UISettings.DataGrid.GroupByOffair))

        Dim gridGroupDescriptions = Me.LiveProgramDataGrid.Items.GroupDescriptions
        Using LiveProgramDataGrid.Items.DeferRefresh()
            gridGroupDescriptions.Clear()

            'For Each groupDesc In groupingItem.GroupDescriptions
            '    gridGroupDescriptions.Add(groupDesc)
            'Next
        End Using
    End Sub

    Private IsClosed As Boolean

    Public Sub Shutdown()
        IsClosed = True
        Me.Close()
    End Sub

    Private Function WindowProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByRef handled As Boolean) As IntPtr
        Select Case msg
            Case &H11, &H16
                ' Windows Shut Down
            Case &H112
                If (wParam.ToInt32 And &HFFF0) = &HF060 Then
                    ' UserClosing
                    If Not IsClosed Then
                        Me.Hide()
                        handled = False ' True
                    Else

                    End If
                End If
        End Select

        Return IntPtr.Zero
    End Function

    Public Sub SetGroup(ByVal group As Grouping.ProgramGroup)

        If group = ProgramGroup.None Then
            Using LiveProgramDataGrid.Items.DeferRefresh()
                Me.LiveProgramDataGrid.Items.GroupDescriptions.Clear()
            End Using
            Exit Sub
        End If

        Dim groupingItem = New Grouping.GroupingItem
        groupingItem.GroupDescriptions.Add(New ProgramGroupDescription(group, _
                                                                       ViewModel.UISettings.DataGrid.GroupByAlertedProgram, _
                                                                       ViewModel.UISettings.DataGrid.GroupByAlertTarget, _
                                                                       ViewModel.UISettings.DataGrid.GroupByOffair))
 
        Dim gridGroupDescriptions = Me.LiveProgramDataGrid.Items.GroupDescriptions
        Using LiveProgramDataGrid.Items.DeferRefresh()
            gridGroupDescriptions.Clear()

            For Each groupDesc In groupingItem.GroupDescriptions
                gridGroupDescriptions.Add(groupDesc)
            Next
        End Using

    End Sub

    'Private Sub GroupComboSelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    '    Dim groupingItem As Grouping.GroupingItem = TryCast(Me.groupCombo.SelectedItem, GroupingItem)
    '    Dim gridGroupDescriptions As ObservableCollection(Of GroupDescription) = Me.grid.Items.GroupDescriptions

    '    If (Not groupingItem Is Nothing) AndAlso ((Not groupingItem.Equals(gridGroupDescriptions))) Then
    '        m_updatingGrouping = True

    '        Using Me.grid.Items.DeferRefresh()
    '            gridGroupDescriptions.Clear()

    '            For Each groupDesc As GroupDescription In groupingItem.GroupDescriptions
    '                gridGroupDescriptions.Add(groupDesc)
    '            Next groupDesc
    '        End Using

    '        m_updatingGrouping = False
    '    End If
    'End Sub

    Private Sub RibbonCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)

    End Sub

    Private Sub SearchRibbonCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Me.ViewModel.ClearSearchCommand.Execute(Nothing)
    End Sub

    Private Sub Shutdown_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Me.ViewModel.ShutdownApplicationCommand.Execute(Nothing)
    End Sub

    Private Sub ShowTopmostRibbonCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Me.ViewModel.ShowTopmostCommand.Execute(Nothing)
    End Sub

    Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim c = NotifyIcon.ContextMenu
        Me.Hide()
    End Sub


    Private Sub ShowColumnRibbonCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)

        With ViewModel.UISettings.DataGrid
            IconColumn.Visible = .IconVisible
            TitleColumn.Visible = .TitleVisible
            StationNameColumn.Visible = .StationNameVisible
            CasterColumn.Visible = .CasterVisible
            StartDateTimeColumn.Visible = .StartDateTimeVisible
            VisitorCountColumn.Visible = .VisitorCountVisible
            CommentCountColumn.Visible = .CommentCountVisible
            OfficialTagColumn.Visible = .OfficialTagVisible
            ShortContentColumn.Visible = .ShortContentVisible
            RatingColumn.Visible = .RatingVisible
            AlertSettingsColumn.Visible = .AlertSettingsVisible
        End With

    End Sub

End Class
